package com.yuncheng.spcyApi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SpcyRsYwzdxx;
import com.yuncheng.spcyApi.service.ISpcyRsYwzdxxService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 审评查验-业务字段选项
 * @Author: yuncheng
 * @Date: 2023-03-29
 * @Version: V1.0
 */
@Api(tags = "审评查验-业务字段选项接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyRsYwzdxx")
public class SpcyRsYwzdxxController extends BaseController<SpcyRsYwzdxx, ISpcyRsYwzdxxService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyRsYwzdxxController.class);

    @Resource
    private ISpcyRsYwzdxxService spcyRsYwzdxxService;

    @Resource
    private ISpcyLogService spcyLogService;

    /**
     * 分页列表查询
     *
     * @param spcyRsYwzdxx
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Log(title = "业务字段选项-分页列表查询")
    @ApiOperation(value = "业务字段选项-分页列表查询")
    @GetMapping(value = "/queryPagelist")
    public HttpResult<?> queryPageList(SpcyRsYwzdxx spcyRsYwzdxx,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyRsYwzdxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyRsYwzdxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(spcyRsYwzdxx.getFywflcode()),SpcyRsYwzdxx::getFywflcode,spcyRsYwzdxx.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx1()),SpcyRsYwzdxx::getFzdlx1,spcyRsYwzdxx.getFzdlx1());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx2()),SpcyRsYwzdxx::getFzdlx2,spcyRsYwzdxx.getFzdlx2());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx3()),SpcyRsYwzdxx::getFzdlx3,spcyRsYwzdxx.getFzdlx3());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx4()),SpcyRsYwzdxx::getFzdlx4,spcyRsYwzdxx.getFzdlx4());

        queryWrapper.orderByDesc(SpcyRsYwzdxx::getCreateTime);
        IPage<SpcyRsYwzdxx> pageList = spcyRsYwzdxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "列表查询-通过业务分类编码和字段类型")
    @ApiOperation(value = "列表查询-通过业务分类编码和字段类型")
    @GetMapping(value = "/queryListYwfl")
    public HttpResult<?> queryListYwfl(SpcyRsYwzdxx spcyRsYwzdxx) {
        LambdaQueryWrapper<SpcyRsYwzdxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(spcyRsYwzdxx.getFywflcode()),SpcyRsYwzdxx::getFywflcode,spcyRsYwzdxx.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx1()),SpcyRsYwzdxx::getFzdlx1,spcyRsYwzdxx.getFzdlx1());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx2()),SpcyRsYwzdxx::getFzdlx2,spcyRsYwzdxx.getFzdlx2());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx3()),SpcyRsYwzdxx::getFzdlx3,spcyRsYwzdxx.getFzdlx3());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx4()),SpcyRsYwzdxx::getFzdlx4,spcyRsYwzdxx.getFzdlx4());

        queryWrapper.orderByDesc(SpcyRsYwzdxx::getCreateTime);
        List<SpcyRsYwzdxx> pageList = spcyRsYwzdxxService.list(queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "通过业务分类编码和字段类型的内容")
    @ApiOperation(value = "通过业务分类编码和字段类型的内容")
    @GetMapping(value = "/queryYwzdYwflContent")
    public HttpResult<?> queryYwzdYwflContent(SpcyRsYwzdxx spcyRsYwzdxx) {
        Map map = new HashMap();
        String content = "";

        LambdaQueryWrapper<SpcyRsYwzdxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(spcyRsYwzdxx.getFywflcode()),SpcyRsYwzdxx::getFywflcode,spcyRsYwzdxx.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx1()),SpcyRsYwzdxx::getFzdlx1,spcyRsYwzdxx.getFzdlx1());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx2()),SpcyRsYwzdxx::getFzdlx2,spcyRsYwzdxx.getFzdlx2());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx3()),SpcyRsYwzdxx::getFzdlx3,spcyRsYwzdxx.getFzdlx3());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsYwzdxx.getFzdlx4()),SpcyRsYwzdxx::getFzdlx4,spcyRsYwzdxx.getFzdlx4());
        queryWrapper.orderByDesc(SpcyRsYwzdxx::getCreateTime);
        List<SpcyRsYwzdxx> list = spcyRsYwzdxxService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)){
            content = list.get(0).getFzdnr();
        }

        map.put("content",content);
        return HttpResult.ok(map);
    }

    /**
     * 添加
     *
     * @param spcyRsYwzdxx
     * @return
     */
    @Log(title = "业务字段选项-添加")
    @ApiOperation(value = "业务字段选项-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody SpcyRsYwzdxx spcyRsYwzdxx) {
        spcyRsYwzdxx.setId(GetUuIdUtils.ReplaceUuId());
        spcyRsYwzdxxService.save(spcyRsYwzdxx);

        String logNr = "内容添加：";
        Map<String, Object> map = ObjectToMapUtils.objectToMap(spcyRsYwzdxx);
        LogUtils.MapToStringMethod(map);
        spcyLogService.addLogs(LogConstant.LOGS_RS_YWZDXX,logNr,spcyRsYwzdxx.getId(),LogConstant.LOGS_RS_YWZDXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "业务字段选项-修改")
    @ApiOperation(value = "业务字段选项-修改")
    @PostMapping(value = "update")
    public HttpResult<?> update(@RequestBody SpcyRsYwzdxx spcyRsYwzdxx){
        if (StringUtils.isBlank(spcyRsYwzdxx.getId())){
            return HttpResult.error("主要参数不能为空，请联系管理员");
        }
        SpcyRsYwzdxx ywzdxx = spcyRsYwzdxxService.getById(spcyRsYwzdxx.getId());
        if (ywzdxx == null){
            return HttpResult.error("当前数据不存在，请刷新重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(ywzdxx);
        ConvertUtils.register(new DateConvert(),java.util.Date.class);
        try {
            BeanUtils.populate(ywzdxx,BeanUtils.describe(spcyRsYwzdxx));
        } catch (IllegalAccessException e) {
            log.error("修改字段出错【IllegalAccessException】: {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("修改字段出错【InvocationTargetException】: {}",e.getMessage());
        } catch (NoSuchMethodException e) {
            log.error("修改字段出错【NoSuchMethodException】: {}",e.getMessage());
        }
        spcyRsYwzdxxService.updateById(ywzdxx);

        String operContent = "业务字段-基本信息修改：";

        operContent += LogUtils.BeanModifyMsg(ywzdxx,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_RS_YWZDXX,operContent,spcyRsYwzdxx.getId(),LogConstant.LOGS_RS_YWZDXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("保存成功");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Log(title = "业务字段选项-通过id删除")
    @ApiOperation(value = "业务字段选项-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        SpcyRsYwzdxx ywzdxx = spcyRsYwzdxxService.getById(id);
        if (ywzdxx != null) {
            spcyRsYwzdxxService.removeById(id);

            String logNr = "内容删除：";
            Map<String, Object> map = ObjectToMapUtils.objectToMap(ywzdxx);
            LogUtils.MapToStringMethod(map);
            spcyLogService.addLogs(LogConstant.LOGS_RS_YWZDXX,logNr,id,LogConstant.LOGS_RS_YWZDXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        }

        return HttpResult.ok("删除成功");
    }


}
